package gin_frame

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

// REST API 统一响应格式规范，0表示失败，1表示成功
const (
	ERROR   = 0
	SUCCESS = 1
)

type Response struct {
	Code int         `json:"code"` //0错误 1成功
	Data interface{} `json:"data"`
	Msg  string      `json:"msg"`
}

func Result(code int, data interface{}, msg string) *Response {
	return &Response{
		code,
		data,
		msg,
	}
}

func Ok() *Response {
	return Result(SUCCESS, map[string]interface{}{}, "操作成功")
}

func OkWithMessage(message string) *Response {
	return Result(SUCCESS, map[string]interface{}{}, message)
}

func OkWithData(data interface{}) *Response {
	return Result(SUCCESS, data, "操作成功")
}

func OkWithDetailed(data interface{}, message string) *Response {
	return Result(SUCCESS, data, message)
}

func Fail() *Response {
	return Result(ERROR, map[string]interface{}{}, "操作失败")
}

func FailWithMessage(message string) *Response {
	return Result(ERROR, map[string]interface{}{}, message)
}

func FailWithDetailed(data interface{}, message string) *Response {
	return Result(ERROR, data, message)
}

// Convert 在路由中使用
func Convert(f func(*gin.Context) *Response) gin.HandlerFunc {
	return func(c *gin.Context) {
		resp := f(c)
		c.JSON(http.StatusOK, resp)
	}
}
